
ols_treatment_interaction <- function(dependent_variable, 
                                      data,
                                      weights,
                                      interaction_term
                                      ){
  # allowing weights to be passed as a column
  data[,"weights"] <- data[,weights]
  
  no_dems_form <- as.formula(paste0(dependent_variable, 
                                    "~ treatment_w21 * ",
                                    interaction_term))
  no_controls <- lm(no_dems_form,
                    data = data,
                    weights = weights)

  with_dems_form <- formula(paste0(dependent_variable, 
                                   " ~ ",
                                   paste0(controls_vec, collapse = " + "),
                                   " + treatment_w21 * ",
                                   interaction_term))
  
  with_controls <- lm(with_dems_form,
                      data = data,
                      weights = weights)

  return(list(no_controls = no_controls, 
              with_controls = with_controls))
}

ols_treatment_no_controls <- function(dependent_variable, 
                                             data,
                                             weights,
                                             interaction_term
                                             ){
  # allowing weights to be passed as a column
  data[,"weights"] <- data[,weights]
  no_dems_form <- as.formula(paste0(dependent_variable, 
                                    "~ treatment_w21 * ",
                                    interaction_term))
  model <- lm(no_dems_form,
              data = data,
              weights = weights)
  return(model)
}
ols_treatment_with_controls <- function(dependent_variable,
                                               data,
                                               interaction_term,
                                               weights){
  data[,"weights"] <- data[,weights]
  with_dems_form <- formula(paste0(dependent_variable, 
                                   " ~ ",
                                   paste0(controls_vec, collapse = " + "),
                                   " + treatment_w21 * ",
                                   interaction_term))
  model <- lm(with_dems_form,
              data = data,
              weights = weights)
  return(model)
}

get_margins <- function(margin_model, data_for_margins){
  neat_margins <- margins(model = margin_model, 
                          data = data_for_margins,
                          variables = c("treatment_w21")) %>%
    summary() %>%
    as_tibble() 
  return(neat_margins)
}

get_margins_gov_op_intx <- function(model, data, dvar){
  gov_partisan <- get_margins(model, 
      data  = (data %>% dplyr::filter(party_id_gov_op_w21 == 
                      "Government partisan"))) %>%
    make_dwplot_compliant() %>%
    mutate(term = "Government partisan")
  opposition_partisan <- get_margins(model, 
      data  = (data %>% dplyr::filter(party_id_gov_op_w21 == 
                      "Opposition partisan"))) %>%
    make_dwplot_compliant() %>%
    mutate(term = "Opposition partisan")
  non_partisan <- get_margins(model, 
      data  = (data %>% dplyr::filter(party_id_gov_op_w21 == 
                      "Non-partisan"))) %>%
    make_dwplot_compliant() %>%
    mutate(term = "Non-partisan")
  return(
    bind_rows(gov_partisan,
              opposition_partisan,
              non_partisan) %>%
      mutate(dvar = dvar) %>%
      renaming_terms_for_plot() %>%
      left_join(dvars, by = c("dvar" = "variables")))
}

make_dwplot_compliant <- function(tibble_of_results){

  compliant_tibble <- tibble_of_results %>%
    rename(model = factor,
           estimate = AME,
           conf.low = lower,
           conf.high = upper)
  
  return(compliant_tibble)
}

renaming_terms_for_plot <- function(compliant_tibble){

  neat_tibble <- compliant_tibble %>%
      mutate(model = case_when(
        str_detect(model, "negative") ~ "Negative treatment",
        str_detect(model, "positive") ~ "Positive treatment",
        str_detect(model, "control")  ~ "Control treatment",
        TRUE ~ "unlabelled")) 
  
  return(neat_tibble)
}

generic_tidy_texreg <- function(list_of_models, ...){
  nice_outputs <- texreg(list_of_models, 
    use.packages = FALSE,
    booktabs = TRUE,
    dcolumn = TRUE,
    caption.above = TRUE,
    float.pos = "ht!",
    ...)
  
  return(nice_outputs)
}

